﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.Persist;
using EffiProz.Core.RowIO;
using EffiProz.Core.Lib;

namespace EffiProz.Core
{
    public class Row : CachedObject
    {

        public int tableId;
        public int position;
        protected Object[] rowData;
        public volatile RowAction rowAction;
        public volatile bool hasAction;

        public RowAction getAction()
        {
            return rowAction;
        }

        /**
         *  Default constructor used only in subclasses.
         */
        protected Row() { }

        /**
         * Returns the array of fields in the database row.
         */
        virtual public Object[] getData()
        {
            return rowData;
        }

        virtual public bool isDeleted(Session session)
        {

            RowAction action = rowAction;

            if (action == null)
            {
                return false;
            }

            return RowActionBase.ACTION_DELETE
                   == action.getLastChangeActionType(session.actionTimestamp);
        }

        virtual public void setChanged() { }

        virtual public void setStorageSize(int size) { }

        virtual public int getStorageSize()
        {
            return 0;
        }

        virtual public long getId()
        {
            return ((long)tableId << 32) + ((long)position);
        }

        virtual public Object getRowidObject()
        {
            return (getId());
        }

        virtual public bool isMemory()
        {
            return true;
        }

        virtual public void updateAccessCount(int count) { }

        virtual public int getAccessCount()
        {
            return 0;
        }

        virtual public int getPos()
        {
            return position;
        }

        virtual public void setPos(int pos)
        {
            position = pos;
        }

        virtual public bool hasChanged()
        {
            return false;
        }

        virtual public bool isKeepInMemory()
        {
            return true;
        }

        virtual public bool keepInMemory(bool keep)
        {
            return true;
        }

        virtual public bool isInMemory()
        {
            return true;
        }

        virtual public void setInMemory(bool _in) { }

        virtual public void restore() { }

        virtual public void destroy()
        {



            rowData = null;
        }

        virtual public int getRealSize(RowOutputInterface _out)
        {
            return 0;
        }

        virtual public TableBase getTable()
        {
            return null;
        }

        virtual public void write(RowOutputInterface _out) { }

        virtual public void write(RowOutputInterface _out, IntLookup lookup) { }

        /**
         * Lifetime scope of this method is limited depends on the operations
         * performed. Rows deleted completely can equal rows produced later.
         * This can return invalid results if used with deleted rows.
         *
         * @param obj row to compare
         * @return boolean
         */
        public override bool Equals(Object obj)
        {

            if (obj == this)
            {
                return true;
            }

            if (obj is Row)
            {
                return ((Row)obj).position == position;
            }

            return false;
        }

        /**
         * Hash code is always valid.
         *
         * @return file position of row
         */
        public override int GetHashCode()
        {
            return position;
        }
    }
}
